package com.gtq.uhome.controller;


import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.MyUhomeService;
import com.gtq.uhome.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping(value = "/zhouzhouController")
@Api("周恩接口")
public class ZhouzhouController {

    @Resource
    private MyUhomeService myUhomeService;

    /**
     * 订单详情页(查询订单状态)
     * @return
     */
    @PostMapping(value = "/selectOrderSatus")
    @ResponseBody
    @ApiOperation(value="用户查询订单的状态",notes = "订单状态的参数",tags = "selectOrderSatus",response = List.class,httpMethod = "POST")
    public Result<List<DeliveryOrderStatusVo> >selectOrderSatus(@RequestBody SignInStatusVo signInStatus){

        List<DeliveryOrderStatusVo> deliveryOrderStatuses = myUhomeService.selectOrderSatus(signInStatus);

        return  Result.ok(deliveryOrderStatuses);

    }
     /**
     * 我的申请
     * @return
     */
    @PostMapping(value = "/selectMyApplication")
    @ResponseBody
    @ApiOperation(value="用户查询我的申请",notes = "订单申请状态参数",tags = "selectMyApplication",response = List.class,httpMethod = "POST")
    public Result<List<MyApplicationVo>> selectOperator(@RequestBody ConditionQueryVo conditionQuery){
        List<MyApplicationVo> myApplications = myUhomeService.selectMyApplication(conditionQuery);

        return  Result.ok(myApplications);

    }

    /**
     *
     * @param updateConsignee 修改收货人的收货信息
     * @return 影响的行数
     *
     */
    @PutMapping(value = "/updateConsignee")
    @ResponseBody
    @ApiOperation(value="修改收货人信息",notes = "需要更改的信息,和订单编号",tags = "update",response = UpdateConsigneeVo.class,httpMethod = "put")
    public Result update(@RequestBody UpdateConsigneeVo updateConsignee){

        Integer update = myUhomeService.update(updateConsignee);
        if (update>=1){

            return  Result.ok();
        }
        return Result.failed("修改失败,请检查");
    }

    /**
     * 配送员端口:查看订单详情
     * @param :前端传回的订单编号
     * @param :前端传回的订单状态
     * @return
     */
    @PostMapping(value = "/selectMarkiOrder")
    @ResponseBody
    @ApiOperation(value="配送员查看订单信息",notes = "订单号和订单状态",tags = "selectMarkiOrder",response = Map.class,httpMethod = "POST")
    public Result<Map<Object, Object>> selectMarkiOrder(@RequestBody Map map){
        String orderId = (String)map.get("orderId");
        String orderStatus = (String)map.get("orderStatus");
        System.out.println("orderId"+orderId+"orderStatus"+orderStatus);
        //查询收件人的信息
        List<ShippingOrderVo> shippingOrders = myUhomeService.selectShippingOrder(orderId,orderStatus);
        //查询生鲜订单信息
        Map<String, Object> stringObjectMap = myUhomeService.selectShippingOrdersFresh(orderId,orderStatus);
        //查询常温订单信息
        Map<String, Object> stringObjectMap1 = myUhomeService.selectShippingOrdersTemperature(orderId,orderStatus);
        Map<Object, Object> objectObjectHashMap = new HashMap<>();
        //将查询到的所有信息返回给前端
        objectObjectHashMap.put("consignee",shippingOrders);
        objectObjectHashMap.put("fresh", stringObjectMap);
       objectObjectHashMap.put("temperature", stringObjectMap1);
        System.out.println(objectObjectHashMap);
        if(shippingOrders.size()>0){
            return  Result.ok(objectObjectHashMap);
        }
        return Result.failed("查询订单信息失败");

    }

    /**
     *  配送员接单成功,更改订单的状态为配送中
     * @param signInStatus
     * @return
     */
    @PostMapping(value="pickUpGoods")
    @ResponseBody
    public Result pickUpGoods(@RequestBody SignInStatusVo signInStatus){
        Boolean aboolean=false;
        Integer integer = myUhomeService.updateOrder(signInStatus);
      if(integer==2){
          Integer integer1 = myUhomeService.insertMsg(signInStatus);
          if (integer1==1){
              aboolean=true;
          }
      }
        return aboolean?Result.ok("操作完成"):Result.failed("操作失败");
    }

}
